﻿using Nasty.Core.LoadParams;
using Nasty.Core.Registry;
using Nasty.Core.Entity.System;
using OfficeOpenXml.ConditionalFormatting.Contracts;
using Nasty.Core.Entity.Flow;
using Nasty.Core.Model;
using Nasty.Core.Entity;
using Nasty.Core.SuperExtension;
using Nasty.FlowModule.FlowTest;
using static Dm.net.buffer.ByteArrayBuffer;
using SqlSugar;

namespace Nasty.FlowModule.FlowAction
{
    public class BTestFlowAction : IAutofacFlowAction
    {
        public SqlSugarClient Db { get; set; }

        public ResultData<string> Start(Core.Entity.Flow.Flow flow, FlowNode node)
        {
            var result = new ResultData<string>();

            try
            {
                var data = Db.Queryable<FlowTest.FlowTest>().InSingle(flow.Id);

                data.FlowStatusName = FlowStatus.Start.Name;
                data.FlowStatusCode = FlowStatus.Start.Code;

                Db.Update(data);

                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public ResultData<string> End(Core.Entity.Flow.Flow flow, FlowNode node)
        {
            var result = new ResultData<string>();

            try
            {
                var data = Db.Queryable<FlowTest.FlowTest>().InSingle(flow.Id);

                data.FlowStatusName = FlowStatus.End.Name;
                data.FlowStatusCode = FlowStatus.End.Code;

                Db.Update(data);

                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public ResultData<string> Agree(Core.Entity.Flow.Flow flow, FlowNode currentNode, FlowNode? nextNode, User user)
        {
            var result = new ResultData<string>();

            try
            {
                if (nextNode != null)
                {
                    var data = Db.Queryable<FlowTest.FlowTest>().InSingle(flow.Id);
                    data.FlowStatusName = FlowStatus.Approval.Name;
                    data.FlowStatusCode = FlowStatus.Approval.Code;

                    Db.Update(data);
                }

                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public ResultData<List<FlowNode>> Branch(Nasty.Core.Entity.Flow.Flow flow, FlowNode currentNode, List<FlowNode> nextNodes)
        {
            var result = new ResultData<List<FlowNode>>();
            try
            {
                // 模拟分支逻辑
                result.Data = nextNodes;
                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }

            return result;
        }

        public ResultData<FlowCandidateItemModel> Candidate(Nasty.Core.Entity.Flow.Flow flow, FlowNode currentNode, FlowNode nextNode)
        {
            var result = new ResultData<FlowCandidateItemModel>();

            try
            {

                // 模拟候选人查询逻辑
                var candidates = new List<User>();

                if (currentNode.Code == "Start")
                {
                    //模拟会签
                    candidates.Add(new User { Id = "1961311590015963136", Name = "test1", Account = "test1" });
                    candidates.Add(new User { Id = "1961311623750750208", Name = "test2", Account = "test2" });
                    candidates.Add(new User { Id = "26f27139-4edf-49ea-ba77-d7a3e78fe4ab", Name = "超级管理员", Account = "admin" });
                }
                else
                {
                    candidates.Add(new User { Id = "26f27139-4edf-49ea-ba77-d7a3e78fe4ab", Name = "超级管理员", Account = "admin" });
                }


                var list = new List<FlowCandidateItemModel>();

                var data = new FlowCandidateItemModel
                {
                    NodeId = nextNode.Id,
                    NodeCode = nextNode.Code,
                    Users = candidates,
                };


                // 模拟候选人逻辑
                result.Data = data;
                result.IsSuccess = true;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public ResultData<string> Reject(Nasty.Core.Entity.Flow.Flow flow, FlowNode currentNode, FlowNode rejectNode)
        {
            var result = new ResultData<string>();

            try
            {
                var data = Db.Queryable<FlowTest.FlowTest>().InSingle(flow.Id);

                data.FlowStatusName = FlowStatus.Reject.Name;
                data.FlowStatusCode = FlowStatus.Reject.Code;

                Db.Update(data);

                result.IsSuccess = true;
                return result;
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public ResultData<string> Revoke(Nasty.Core.Entity.Flow.Flow flow)
        {
            var data = Db.Queryable<FlowTest.FlowTest>().InSingle(flow.Id);

            data.FlowStatusCode = FlowStatus.Wait.Code;
            data.FlowStatusName = FlowStatus.Wait.Name;

            Db.Update(data);

            return new ResultData<string>
            {
                IsSuccess = true,
                Message = "撤销成功"
            };
        }

        public ResultData<string> Delegate(Core.Entity.Flow.Flow flow, FlowNode currentNode, User source, User target)
        {
            return new ResultData<string>
            {
                IsSuccess = true,
                Message = "委派成功"
            };
        }


    }
}
